﻿Imports System.Data.OleDb

Public Class AddRestaurant
    Inherits System.Web.UI.Page
    Dim hasRated As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Set the Calendar date to today by default
        If Not IsPostBack Then
            e_RestaurantLastReviewDate.SelectedDate = DateTime.Now.Date()
        End If

    End Sub

    Protected Sub btn_AddRestaurant_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_AddRestaurant.Click



        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        Dim newPictureName = myGUID.ToString() + ".jpg"
        Dim newMenuName = myGUID.ToString() + ".pdf"

        If f_Picture.HasFile Then
            f_Picture.SaveAs(Request.MapPath("~/Pictures") & "/" & newPictureName)
        End If

        If f_Menu.HasFile Then
            f_Menu.SaveAs(Request.MapPath("~/Menus") & "/" & newMenuName)
        End If

        Dim oleDbConn2 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringRestaurants").ConnectionString)
        Dim SqlString As String = "Insert into Restaurants(RestaurantName,PostCode,Description,Photo,Menu, Rating, LastReviewDate, PriceBracket, URL,UserName) Values (@f1,@f2,@f3,@f4,@f5,@f6, @f7,@f8,@f9,@f10)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn2)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_RestaurantName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_PostCode.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f4", "~/Pictures/" & newPictureName)
        cmd.Parameters.AddWithValue("@f5", "~/Menus/" & newMenuName)
        cmd.Parameters.AddWithValue("@f6", r_Rating.CurrentRating)
        cmd.Parameters.AddWithValue("@f7", tb_RestaurantLastReviewDate.Text)
        cmd.Parameters.AddWithValue("@f8", rbl_PriceBracket.SelectedValue)
        cmd.Parameters.AddWithValue("@f9", tb_URL.Text)
        cmd.Parameters.AddWithValue("@f10", User.Identity.Name)
        oleDbConn2.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("~/confirmation.aspx")


    End Sub
End Class